from django import forms
from studenthoods.apps.common import widgets
from studenthoods.apps.shop.models import Shop, HoodieSize, HoodieColour
from studenthoods.apps.shop.utils import get_shop_slug_hash

class OrderForm(forms.Form):
    slug_hash = forms.CharField(max_length=60, widget=forms.HiddenInput())
    colour = forms.ChoiceField(widget=forms.RadioSelect(), label="Select colour")
    size = forms.ChoiceField(label="Select size")
    first_name = forms.CharField(max_length=15, label="First name")
    surname = forms.CharField(max_length=15, label="Surname")
    email = forms.EmailField(max_length=40, widget=widgets.MyEmailInput)
    phone_number = forms.CharField(max_length=15, label="Phone number")
    address_line_1 = forms.CharField(max_length=25, label="Address line 1")
    address_line_2 = forms.CharField(max_length=25, label="Address line 2")
    city = forms.CharField(max_length=25, label="City")
    postcode = forms.CharField(max_length=10, label="Postcode")
    terms = forms.BooleanField(required=True, label="I have read and accept the <a href='{% url terms_and_conditions %}'>Terms and Conditions</a> and the order information opposite.")
    
    def __init__(self, shop, *args, **kwargs):
        self.validate = kwargs.pop('validate',False)    
        super(OrderForm, self).__init__(*args, **kwargs)
        
        self.fields['slug_hash'].initial = shop.slug_hash
        
        choice_colours = []
        choice_sizes = []

        colours = shop.colours()

        for colour in colours:
            choice_colours += (colour, colour),

        for size in HoodieSize.objects.all().filter(colour__shop=shop):
            choice_sizes +=(size, size),

        self.fields['colour'].choices = choice_colours
        self.fields['size'].choices = choice_sizes

        extra_errors = {
            'size':{'required':'Please select a Size'},
            'colour':{'required':'Please select a Colour'},
            'first_name':{'required':'Please enter your First name'},
            'surname':{'required':'Please enter your Surname'},
            'email':{'required':'Please enter your Email address'},
            'phone_number':{'required':'Please enter your Phone number'},
            'address_line_1':{'required':'Please enter your Address line 1'},
            'address_line_2':{'required':'Please enter your Address line 2'},
            'city':{'required':'Please enter your City'},
            'postcode':{'required':'Please enter your Postcode'},
            'terms':{'required':'Please agree to the Terms and Conditions'},
        }

        if shop.address_required == False:
            self.fields['address_line_1'].required = False
            self.fields['address_line_2'].required = False
            self.fields['city'].required = False
            self.fields['postcode'].required = False

        for field, errors in extra_errors.iteritems():
            self.fields[field].error_messages.update(errors)

    def clean(self):
        slug_hash = str(self.cleaned_data['slug_hash'])
        shop = get_shop_slug_hash(slug_hash)

        try:
            colour = HoodieColour.objects.filter(shop = shop).get(colour = self.cleaned_data['colour'])
        except:
            self._errors["colour"] = self.error_class(['Please select a Colour'])

        try:
            size = HoodieSize.objects.filter(colour=colour).get(size=self.cleaned_data['size'])
        except:
            self._errors["size"] = self.error_class(['Please select a valid Size for your Colour choice'])

        return self.cleaned_data